1   //==============================================================================
2   // file :       XMLDocumentHandlerSAX.java
3   // project:     Lucene Search System
4   //
5   // last change: date:       $Date: 2003/09/09 03:11:52 $
6   //              by:         $Author: bitiboy $
7   //              revision:   $Revision: 1.1 $
8   //------------------------------------------------------------------------------
9   // copyright:   GNU GPL Software License (see class documentation)
10  //==============================================================================
11  
12  package com.justhis.lucene.xml;
13  
14  
15  /*
16   * $Id: XMLDocumentHandlerSAX.java,v 1.1 2003/09/09 03:11:52 bitiboy Exp $
17   *
18   * Copyright 2003 Acai Software All Rights Reserved.
19   *
20   * This file LuceneException.java is part of the Lucene Search System.
21  
22   * The Lucene Search System is free software; you can redistribute it and/or modify
23   * it under the terms of the GNU General Public License as published by
24   * the Free Software Foundation; either version 2 of the License, or
25   * (at your option) any later version.
26  
27   * Lucene Search System is distributed in the hope that it will be useful,
28   * but WITHOUT ANY WARRANTY; without even the implied warranty of
29   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30   * GNU General Public License for more details.
31  
32   * You should have received a copy of the GNU General Public License
33   * along with the Lucene Search System; if not, write to the Free Software
34   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
35  
36   * http://www.justhis.com http://ejb.cn
37   * CONTACT: email = webmaster@justhis.com superaxis@sohu.com
38   */
39  import org.apache.lucene.document.Document;
40  import org.apache.lucene.document.Field;
41  
42  import org.xml.sax.*;
43  import org.xml.sax.helpers.DefaultHandler;
44  
45  import java.io.File;
46  import java.io.IOException;
47  
48  import javax.xml.parsers.*;
49  
50  
51  /***
52   * TODO
53   *
54   * @author <a href="http://blog.ejb.cn">acai</a>
55   * @version $Revision: 1.1 $
56   */
57  public class XMLDocumentHandlerSAX extends DefaultHandler {
58      //~ Instance fields --------------------------------------------------------
59  
60      /*** A buffer for each XML element */
61      private StringBuffer elementBuffer = new StringBuffer();
62  
63      /*** ????????lucene???????? */
64      private Document mDocument;
65  
66      //~ Constructors -----------------------------------------------------------
67  
68      /***
69       * ???????? ????XML??????????????????????SAX????????????????Handler??XML??????????
70       *
71       * @param xmlFile XML????????
72       *
73       * @throws ParserConfigurationException if the implementation is not
74       *         available or cannot be instantiated.
75       * @throws SAXException if a parser cannot be created which satisfies the
76       *         requested configuration.
77       * @throws IOException If any IO errors occur
78       */
79      public XMLDocumentHandlerSAX(File xmlFile)
80                            throws ParserConfigurationException, SAXException, 
81                                   IOException {
82          SAXParserFactory spf = SAXParserFactory.newInstance();
83  
84          SAXParser parser = spf.newSAXParser();
85          parser.parse(xmlFile, this);
86      }
87  
88      //~ Methods ----------------------------------------------------------------
89  
90      /* ???? Javadoc??
91           * @see org.xml.sax.ContentHandler#startDocument()
92           */
93  
94      // call at document start
95      public void startDocument() {
96          mDocument = new Document();
97      }
98  
99      /* ???? Javadoc??
100          * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
101          */
102 
103     // call at element start
104     public void startElement(String uri, String localName, String qName,
105                              Attributes attributes
106                             ) throws SAXException {
107         elementBuffer.setLength(0);
108     }
109 
110     /* ???? Javadoc??
111          * @see org.xml.sax.ContentHandler#characters(char[], int, int)
112          */
113 
114     // call when cdata found
115     public void characters(char[] text, int start, int length) {
116         elementBuffer.append(text, start, length);
117     }
118 
119     /* ???? Javadoc??
120          * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
121          */
122 
123     // call at element end
124     public void endElement(String uri, String localName, String qName)
125                     throws SAXException {
126         System.out.println("field :" + qName + ";field Value:"
127                            + elementBuffer.toString()
128                           );
129         mDocument.add(Field.Text(qName, elementBuffer.toString()));
130     }
131 
132     /***
133      * DOCUMENT ME!
134      *
135      * @return TODO
136      */
137     public Document getDocument() {
138         return mDocument;
139     }
140 }
141 /*
142  * $Log: XMLDocumentHandlerSAX.java,v $
143  * Revision 1.1  2003/09/09 03:11:52  bitiboy
144  * *** empty log message ***
145  *
146  * Revision 1.1  2003/09/09 00:54:45  bitiboy
147  * *** empty log message ***
148  *
149  * Revision 1.1  2003/09/07 08:23:50  superaxis
150  * *** empty log message ***
151  *
152  *
153 */
This page was automatically generated by Maven